/**
 * <p>viewformpanel的控制类</p>
 * 继承自Ext.app.Controller

 * @module Ext-ux-cceaf-controller
 * @class cceaf.controller.ViewFormPanelCtrl
 * @extends Ext.app.Controller
 * @since 1.0
 */
Ext.define('cceaf.controller.ViewFormPanelCtrl',{
	extend: 'Ext.app.Controller', 
	requires: [
	    //'Ext.Array'
    ],
	
    constructor: function(config){
		config = config || {};
		
		config = Ext.applyIf(config || {}, {

		});

		cceaf.controller.ViewFormPanelCtrl.superclass.constructor.apply(this, arguments);
	},

	/**
     * 监听事件
     */
    init: function() {
    	//控制层需控制视图的xtype
    	var controlledViewXType = this.controlledViewXType;
    	if (!controlledViewXType) {
    		//控制名称
    		var className = this.self.getName();
    		alert('必须设置控制层' + className + '需控制视图的xtype，即controlledViewXType属性！');
    		return;
    	}
    	
    	//构建初始化的事件监听
    	var eventTemplate = "this.control({" + 
	    	"'aaaaa': {" +
	    	"	afterrender: this.initPage" +
	        "}," +
        	"'aaaaa #btnPrevious': {" + 
            "    click: this.page_previous" + 
            "}," + 
            "'aaaaa #btnNext': {" + 
            "    click: this.page_next" + 
            "}," + 
            "'aaaaa #btnClose': {" + 
            "    click: this.page_close" + 
            "}" + 
    	"});";
	    
	    var eventString = eventTemplate.replace(/aaaaa/gi, controlledViewXType);
	    eval(eventString);
        
        this.registerEvent();
        
      //标记已经初始化完成
        this.isInit = true;
    },
    
    /**
     * 对于动态加载controller模式
     * 第一次加载后设置为true，再次加载后不在执行init方法 
     * 
     * @property isInit 
     * @type Boolean
     * @default false
     */
    isInit: false,
    
    /**
     * 监听其他事件
     */
    registerEvent: function(){
    	
    },
    
    /**
	 * 关闭弹出页面
	 *
     * @method page_close 
     */
    page_close: function(){
    	var billForm = this.getControlledView();
    	var addAndEditWin = billForm.up('window');
    	if (addAndEditWin) {
    		addAndEditWin.close();
    	}
    },
    
    /**
	 * 获取视图
	 *
     * @return {Ext.container.Container} 视图对象
     */
    getControlledView: function(){
    	var controlledViewXType = this.controlledViewXType;
    	var methodName = 'this.get' + controlledViewXType.substr(0,1).toUpperCase() + controlledViewXType.substring(1) + '()';
    	//alert(methodName);
    	
    	var view = eval(methodName);
    	return view;
    },
    
    /**
     * 设置字段为只读属性（所有field控件有效）
     */
    setReadOnly: function(flag){
    	var controller = this;
   	 	var billForm = controller.getControlledView();
   	 
    	var formFields = billForm.query('.field');
		for(var i=0; formFields != null && i<formFields.length; i++){
    		var item = formFields[i];
    		item.setReadOnly(flag);
    	}
    },
    
    /**
     * 进入页面时，设置初始值（所有field控件失效）
     */
    initPage: function(){
    	var controller = this;
   	 	var billForm = controller.getControlledView();
    	controller.setReadOnly(true);
	    
    	var datas = billForm.datas;
    	if (datas && datas.length > 0) {
    		var pageInfo = billForm.down('#pageInfo');
        	var btnPrevious = billForm.down('#btnPrevious');
        	var btnNext = billForm.down('#btnNext');
        	
    		billForm.currentPageNo = 1;
    		billForm.totalPageCount = datas.length;
    		
    		btnPrevious.setDisabled(true);
    		if (datas.length > 1) {
    			btnNext.setDisabled(false);
    		} else {
    			btnNext.setDisabled(true);
    		}
    		
    		billForm.form.setValues(datas[0]);
        	pageInfo.setText(Ext.util.Format.format("第{0}页 /共 {1}页", billForm.currentPageNo, billForm.totalPageCount));
    	} else {
    		var store = billForm.store;
    	    if (store) {
	    		var params = {
	    			isquery: true
	    		};
	    		store.load({
	    		    scope: this,
	    		    params: params
	    		});
    	    }
    	}
    },
    
    /**
     * 导航到上一页
     */
     page_previous: function(){
     	var controller = this;
   	 	var billForm = controller.getControlledView();
    	
    	var pageInfo = billForm.down('#pageInfo');
    	var btnPrevious = billForm.down('#btnPrevious');
    	var btnNext = billForm.down('#btnNext');
    	
    	var datas = billForm.datas;

     	billForm.form.reset();
     	
//     	alert('billForm.currentPageNo=' + billForm.currentPageNo);
//     	alert('billForm.totalPageCount=' + billForm.totalPageCount);
//     	alert('datas.length=' + datas.length);
     	
     	if (datas && datas.length > 0) {
     		billForm.currentPageNo -= 1;
     		
     		if (billForm.currentPageNo < datas.length) {
     			btnNext.setDisabled(false);
     		} else {
     			btnNext.setDisabled(true);
     		}
     		if (billForm.currentPageNo > 1) {
     			btnPrevious.setDisabled(false);
     		} else {
     			btnPrevious.setDisabled(true);
     		}
     		
     		billForm.form.setValues(datas[billForm.currentPageNo-1]);
 			
     		var detailDataName = billForm.detailDataName;
     		if (detailDataName) {
     			var detailDatas = eval("datas[" + (billForm.currentPageNo-1) + "]." + detailDataName);
     			dataForm.down('cceafviewgridpanel').getStore().removeAll();
 	    		dataForm.down('cceafviewgridpanel').getStore().loadData(detailDatas,true);
     		}
     	}
     	pageInfo.setText(Ext.util.Format.format("第{0}页 /共 {1}页", billForm.currentPageNo, billForm.totalPageCount));
     },
     
     /**
     * 导航到下一页
     */
     page_next: function(){
    	 var controller = this;
    	 var billForm = controller.getControlledView();
     	
     	var pageInfo = billForm.down('#pageInfo');
     	var btnPrevious = billForm.down('#btnPrevious');
     	var btnNext = billForm.down('#btnNext');
     	
     	var datas = billForm.datas;
     	
     	billForm.form.reset();

     	if (datas && datas.length > 0) {
     		if (billForm.currentPageNo == datas.length) {
     			
//     			alert('billForm.currentPageNo=' + billForm.currentPageNo);
//     	     	alert('billForm.totalPageCount=' + billForm.totalPageCount);
//     	     	alert('datas.length=' + datas.length);
     			
     			var store = billForm.store;
        	    if (store) {
    	    		var params = {
    	    			isquery: false,
    	    			start: billForm.currentPageNo,
    	    			limit: szpshang.Global.PAGE_SIZE,
    	    			totalCount: billForm.totalPageCount
    	    		};
    	    		store.load({
    	    		    scope: this,
    	    		    params: params
    	    		});
        	    }
        	    return;
     		}
     		
     		billForm.currentPageNo += 1;
     		
     		if (billForm.currentPageNo < billForm.totalPageCount) {
     			btnNext.setDisabled(false);
     		} else {
     			btnNext.setDisabled(true);
     		}
     		if (billForm.currentPageNo > 1) {
     			btnPrevious.setDisabled(false);
     		} else {
     			btnPrevious.setDisabled(true);
     		}
     		
     		billForm.form.setValues(datas[billForm.currentPageNo-1]);
 			
     		var detailDataName = billForm.detailDataName;
     		if (detailDataName) {
     			var detailDatas = eval("datas[" + (billForm.currentPageNo-1) + "]." + detailDataName);
     			dataForm.down('cceafviewgridpanel').getStore().removeAll();
 	    		dataForm.down('cceafviewgridpanel').getStore().loadData(detailDatas,true);
     		}
     	}
     	pageInfo.setText(Ext.util.Format.format("第{0}页 /共 {1}页", billForm.currentPageNo, billForm.totalPageCount));
     }
    
});